package com.shouhuobao.bhi.loc;

import java.math.BigDecimal;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.collectplus.express.BaseActivity2;
import com.collectplus.express.R;
import com.collectplus.express.logic.AppHttp;
import com.collectplus.express.logic.AppResult;
import com.collectplus.express.logic.JSON;
import com.collectplus.express.logic.MsgID;
import com.collectplus.express.model.LocationBean;

import droid.frame.activity.HandlerMgr;
import droid.frame.activity.title.TitleRes;

/**
 * 定位-确认界面
 * 
 * @author coffee<br>
 *         2015-6-30下午2:48:47
 */
public class LocationSaveActivity extends BaseActivity2 {

	private EditText mCurrentEdit, mDetailEdit;
	// 经纬度
	private double mLatitude, mLongitude;
	// 平面
	private BigDecimal mX, mY;

	private LocationBean location;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void findViewById() {
		setContentView(R.layout.address_perfect);
		super.findViewById();

		getAppTitle().setCommonTitle("完善地址", new TitleRes(0, "确定", new View.OnClickListener() {
			public void onClick(View v) {
				String communityName = mCurrentEdit.getText().toString();
				String address = mDetailEdit.getText().toString();
				//
				if (TextUtils.isEmpty(communityName)) {
					showToast("楼号不能为空");
					return;
				}
				if (TextUtils.isEmpty(address)) {
					showToast("单元号不能为空");
					return;
				}
				if (isEmpty(mX) && isEmpty(mY)) {
					showToast("获取坐标失败, 请重试");
				} else {
					showLoadingDialog(null);
					location = new LocationBean(communityName, address, mLongitude + "", mLatitude + "", mX, mY);
					Message msg = new Message();
					msg.what = MsgID.save_address;
					HandlerMgr.sendMessage(msg, 0);
				}
			}
		}));

		mCurrentEdit = (EditText) findViewById(R.id.perfect_address_current);
		mDetailEdit = (EditText) findViewById(R.id.perfect_address_detail);

		String name = getIntent().getStringExtra("name");
		if (name.equals("当前位置")) {
			mCurrentEdit.setText(getIntent().getStringExtra("address"));
		} else {
			mCurrentEdit.setText(getIntent().getStringExtra("name"));
		}
		mLatitude = getIntent().getDoubleExtra("latitude", 0);
		mLongitude = getIntent().getDoubleExtra("longitude", 0);

		showLoadingDialog(null);
		AppHttp.getInstance().mapGeoconv(mLongitude, mLatitude);
	}

	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case MsgID.save_address:
			cancelLoadingDialog();
			final AppResult<?> result = JSON.parser(msg.obj);
			result.setStatus(1);
			switch (result.getStatus()) {
			case 1:
				Intent intent = new Intent();
				intent.putExtra("item", location);
				setResult(Activity.RESULT_OK, intent);
				finish();
				break;
			case 0:
				showToast(result.getMessage());
				break;
			case -1:
				// showToast("超出服务区");
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						// Intent intent = new Intent();
						// intent.setClass(getContext(), WebViewDialogActivity.class);
						// intent.putExtra("from", "region");
						// intent.putExtra("url", result.getMessage());
						// startActivity(intent);
					}
				});
				break;
			}
			return true;
		case MsgID.map_geoconv:
			cancelLoadingDialog();
			try {
				JSONObject json = new JSONObject(msg.obj.toString());
				JSONObject pointResult = json.getJSONArray("result").getJSONObject(0);
				//
				mX = new BigDecimal(pointResult.getString("x"));
				mY = new BigDecimal(pointResult.getString("y"));
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return true;
		default:
			return super.handleMessage(msg);
		}

	}
}
